package model_layer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Class used for managing the data of restocking.
 */
public class Restock
{
	
	private Date receivement_date;
	private int id;
	private float total_price;
	private List<ProductPart> products;
	private Supplier supplier;
	
	/**
	 * Instantiates a new restock.
	 */
	public Restock()
	{
		products = new ArrayList<ProductPart>();
	}

	/**
	 * Gets the date of receiving.
	 *
	 * @return the date of type Date
	 */
	public Date getReceivement_date()
	{
		return receivement_date;
	}

	/**
	 * Gets the id.
	 *
	 * @return the id
	 */
	public int getId()
	{
		return id;
	}

	/**
	 * Gets the total_price.
	 *
	 * @return the total_price
	 */
	public float getTotal_price()
	{
		return total_price;
	}

	/**
	 * Gets the products.
	 *
	 * @return the products
	 */
	public List<ProductPart> getProducts()
	{
		return products;
	}

	/**
	 * Gets the supplier.
	 *
	 * @return the supplier
	 */
	public Supplier getSupplier()
	{
		return supplier;
	}

	/**
	 * Sets the receivement_date.
	 *
	 * @param receivement_date the new receivement_date
	 */
	public void setReceivement_date(Date receivement_date)
	{
		this.receivement_date = receivement_date;
	}

	/**
	 * Sets the id.
	 *
	 * @param id the new id
	 */
	public void setId(int id)
	{
		this.id = id;
	}

	/**
	 * Sets the total_price.
	 *
	 * @param total_price the new total_price
	 */
	public void setTotal_price(float total_price)
	{
		this.total_price = total_price;
	}

	/**
	 * Sets the products.
	 *
	 * @param products the new products
	 */
	public void setProducts(List<ProductPart> products)
	{
		this.products = products;
	}

	/**
	 * Sets the supplier.
	 *
	 * @param supplier the new supplier
	 */
	public void setSupplier(Supplier supplier)
	{
		this.supplier = supplier;
	}
	
	/**
	 * Add_product_part.
	 *
	 * @param part the part
	 */
	public void add_product_part(ProductPart part)
	{
		products.add(part);
	}
	
	/**
	 * Calculate_price.
	 */
	public void calculate_price()
	{
		total_price = 0;
		for (ProductPart part : products)
		{
			total_price += part.getTotal_price();
		}
	}
	
	/**
	 * Gets the product part by id.
	 *
	 * @param id the id
	 * @return the product part.
	 */
	public ProductPart get_product_part_by_id(String id)
	{
		for (ProductPart part : products)
		{
			if (part.getProduct().getId().equals(id))
			{
				return part;
			}
		}
		return null;
	}
	
	/**
	 * Gets the  product part by name.
	 *
	 * @param name the name
	 * @return the product part
	 */
	public ProductPart get_product_part_by_name(String name)
	{
		for (ProductPart part : products)
		{
			if (part.getProduct().getName().equals(name))
			{
				return part;
			}
		}
		return null;
	}
	
	/**
	 * Method for checking if a product is already inserted and returning one if there is such.
	 *
	 * @param old_id the id of the product
	 * @param name its name
	 * @return the Objec of type ProductPart and null if there is no such.
	 */
	public ProductPart check_for_repeated_insert(String old_id, String name)
	{
		for (ProductPart part : products)
		{
			if(part.getProduct().getName().equals(name) && part.getProduct().getId().equals(old_id))
			{
				return part;
			}
		}
		return null;
	}
	
	/**
	 * Updates a product by given id
	 *
	 * @param old_id the id of the specific product
	 * @param prod the newly created product with new details.
	 * @return the updated product part
	 */
	public ProductPart update_product(String old_id, Product prod)
	{
		{
			for(ProductPart part : products)
			{
				if(prod.getId().equals(part.getProduct().getId()))
				{
					return null;
				}
			}
			for (ProductPart part : products)
			{
				if (part.getProduct().getId().equals(old_id))
				{
					part.setProduct(prod);
					part.calculate_price();
					calculate_price();
					return part;
				}
			}
			System.err.println("Shouldn't go that far - updating product in restock");
			return null;
		}
	}
	
	/**
	 * Updating amount of certain product in restocking.
	 *
	 * @param id the id of the product
	 * @param amount the amount of products
	 * @return the product part
	 */
	public ProductPart update_amount(String id, int amount)
	{
		for (ProductPart part : products)
		{
			if(part.getProduct().getId().equals(id))
			{
				part.setAmount(amount);
				part.calculate_price();
				calculate_price();
				return part;
			}
		}
		System.err.println("Shouldn't go that far - update amount in restock");
		return null;
	}
	
	/**
	 * Updates the retail price of a product.
	 *
	 * @param id the id
	 * @param retail_price the retail_price
	 * @return the product part
	 */
	public ProductPart update_retail_price(String id, float retail_price)
	{
		for (ProductPart part : products)
		{
			if(part.getProduct().getId().equals(id))
			{
				part.setRetail_price(retail_price);
				part.calculate_price();
				calculate_price();
				return part;
			}
		}
		System.err.println("Shouldn't go that far - update retail_price in restock");
		return null;
	}
	
	/**
	 * Updates the profit of a product.
	 *
	 * @param id the id
	 * @param profit the profit
	 * @return the product part
	 */
	public ProductPart update_profit(String id, float profit)
	{
		for (ProductPart part : products)
		{
			if(part.getProduct().getId().equals(id))
			{
				float retail_price = part.getRetail_price();
				part.getProduct().setPrice(profit * retail_price / 100 + retail_price);
				part.calculate_price();
				calculate_price();
				return part;
			}
		}
		System.err.println("Shouldn't go that far - update profit in restock");
		return null;
	}
	
	/**
	 * Updates the sale price.
	 *
	 * @param id the id
	 * @param sale_price the sale_price
	 * @return the product part
	 */
	public ProductPart update_sale_price(String id, float sale_price)
	{
		for (ProductPart part : products)
		{
			if(part.getProduct().getId().equals(id))
			{
				part.getProduct().setPrice(sale_price);
				return part;
			}
		}
		System.err.println("Shouldn't go that far - update sale_price in restock");
		return null;
	}
	
	/**
	 * Removes the product part.
	 *
	 * @param id the id of the product
	 */
	public void remove_product_part(String id)
	{
		for (ProductPart part : products)
		{
			if(part.getProduct().getId().equals(id))
			{
				products.remove(part);
				break;
			}
		}
		calculate_price();
	}

}
